<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/common/hospital_token.jsp"%>
<%
    // 标签内删除成员
    Integer labelId = input.getInt("label_id", -1);
    String user = input.getDesc("user", "[]");
    String dept = input.getDesc("dept", "[]");
    List<Integer> userIdList = JSONUtil.fromJson(user, new TypeReference<List<Integer>>(){});
    List<Integer> deptIdList = JSONUtil.fromJson(dept, new TypeReference<List<Integer>>(){});
    if (labelId == -1 || (isEmpty(userIdList) && isEmpty(deptIdList))) {
        ErrcodeUtil.setErrcode(msg, ErrcodeUtil.FAILURE);
        Tools.encryptPrint(out, msg);
        return;
    }
    HospitalLabel hospitalLabel = LabelUtil.getHospitalLabelById(hospital, labelId);
    if (hospitalLabel == null) {
        msg.setErrcodeAndErrmsg(ErrcodeUtil.FAILURE, "标签不存在");
        Tools.encryptPrint(out, msg);
        return;
    }
    Map<String, List<Map>> labelMember = LabelUtil.getLabelMember(hospital, labelId, 0);
    List<Map> userList = labelMember.get("user");
    List<Map> deptList = labelMember.get("dept");
    List<Integer> _userIdList = new ArrayList<Integer>();
    List<Integer> _deptIdList = new ArrayList<Integer>();
    for (Map userMap : userList) {
        Integer userId = Tools.getInt(userMap.get("user_id") + "", -1);
        _userIdList.add(userId);
    }
    for (Map deptMap : deptList) {
        Integer deptId = Tools.getInt(deptMap.get("dept_id") + "", -1);
        _deptIdList.add(deptId);
    }
    List<Integer> $userIdList = new ArrayList<Integer>();
    List<Integer> $deptIdList = new ArrayList<Integer>();
    for (Integer userId : userIdList) {
        if (userId != null && _userIdList.contains(userId)) {
            $userIdList.add(userId);
        }
    }
    for (Integer deptId : deptIdList) {
        if (deptId != null && _deptIdList.contains(deptId)) {
            $deptIdList.add(deptId);
        }
    }
    if (!isEmpty($userIdList)) {
        String userIdStr = LabelUtil.joint($userIdList, ",");
        QBuilder.db("basedb").execHQL("update HospitalLabelMember hlm set hlm.status = " + ConstantUtil.STATUS_DISABLE + " where hlm.hospital = " + hospital + " and hlm.status = " + ConstantUtil.STATUS_ENABLE + " and hlm.labelId = " + labelId + " and hlm.type = 1 and hlm.memberId in (" + userIdStr + ")");
    }
    if (!isEmpty($deptIdList)) {
        String deptIdStr = LabelUtil.joint($deptIdList, ",");
        QBuilder.db("basedb").execHQL("update HospitalLabelMember hlm set hlm.status = " + ConstantUtil.STATUS_DISABLE + " where hlm.hospital = " + hospital + " and hlm.status = " + ConstantUtil.STATUS_ENABLE + " and hlm.labelId = " + labelId + " and hlm.type = 2 and hlm.memberId in (" + deptIdStr + ")");
    }
    msg.setErrmsg("删除成功");
    Tools.encryptPrint(out, msg);
%>
